$(document).ready(function() {
	$("#demo").validate({
		rules:{
			"courseTeaClassFK.teacherUsername": {
				required: true	    		
			},
			"courseId": {
				required: true
			}
		},
		messages:{
			"courseId":"至少选择1门课程"
		},
		errorPlacement: function(error, element) {   
	        if (element.attr("name") == "courseId" )  {
	            error.appendTo("#errMsg"); 
	        } else{     
	            error.insertAfter(element); 
	        }
		}
	});
});

//初始化教师列表
function initTermList(){
	$.post(
		"../AjaxResultData/listAllTerm.action",
		function(data){
			var termData = eval("("+data.termAllData+")");  
			for(var i = 0;i<termData.length;i++){
				$(".termId").append("<option value='"+termData[i].termId+"'>"+termData[i].termName+"</option>");
			}
		},
		"json"
	);
}

//初始化教师列表
function initTeacherList(){
	$.post(
		"../AjaxResultData/listAllUsers.action",
		function(data){
			var usersData = eval("("+data.usersData+")");  
			for(var i = 0;i<usersData.length;i++){
				$(".teacherUsername").append("<option value='"+usersData[i].userName+"'>"+usersData[i].realName+"("+usersData[i].userName+")</option>");
			}
		},
		"json"
	);
}

//初始化课程列表
function initCourseList(){
	$.post(
		"../AjaxResultData/listAllCourse.action",
		function(data){
			var courseData = eval("("+data.courseData+")");  
			for(var i = 0;i<courseData.length;i++){
				$(".courseId").append("<option value='"+courseData[i].courseId+"'>"+courseData[i].courseName+"</option>");
			}
		},
		"json"
	);
}

//初始化课程班级列表
function initClassList(){
	$.post(
		"../AjaxResultData/listAllClass.action",
		function(data){
			var classData = eval("("+data.classAllData+")");  
			for(var i = 0;i<classData.length;i++){
				$(".classId").append("<option value='"+classData[i].classId+"'>"+classData[i].className+"</option>");
			}
		},
		"json"
	);
}

/*
function changeTeacherList(){
	$("#teacherUsername").change(function(){
		var teacherUsername = document.getElementById("teacherUsername").value;
		//alert(teacherUsername);
		$("#listCourse").empty();
		$.post(
				"../AjaxResultData/listCourseByTUN.action",
				{"courseTeaClassFK.teacherUsername":teacherUsername},
				function(data){
					var courseData = eval("("+data.courseData+")");
					var checkedCourseData =   eval("("+data.checkedCourseData+")");
					for(var i = 0;i<courseData.length;i++){
						var flag = 0;
						for(var j = 0;j<checkedCourseData.length;j++){
							if(checkedCourseData[j].courseId==courseData[i].courseId){
	  							flag = 1;
							}
						}
						if(flag==1){
							$("#listCourse").append("<div class='noMoreLine'><input type='checkbox' name='courseId' value='"+courseData[i].courseId+"' class='no_border' checked='checked'>"+courseData[i].courseName+"("+courseData[i].courseCateName+")</div>");	
						}else{
							$("#listCourse").append("<div class='noMoreLine'><input type='checkbox' name='courseId' value='"+courseData[i].courseId+"' class='no_border'>"+courseData[i].courseName+"("+courseData[i].courseCateName+")</div>");	
						}
					}
				},
				"json"
			);
	});
}
*/

/*function checkbox() {
	var str = document.getElementsByName("courseId");
	var objarray = str.length;
	var collection = "";
	for (i = 0; i < objarray; i++) {
		if (str[i].checked == true) {
			collection += str[i].value + ",";
		}
	}
	if (collection == "") {
		alert("至少选择一门课程");
	} else {
		collection = collection.substring(0, collection.lastIndexOf(','));
		document.getElementById("courseTeaClassFK.courseCollection").value = collection;
	}
}*/